home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / AmigaTalk / general / Interval.st < prev    next >
Text File  |  2000-02-14  |  2KB  |  62 lines

  1. Class Interval :SequenceableCollection
  2. ! lower upper step current !
  3. [
  4.    from: lowerBound to: upperBound by: stepSize
  5.      current <- lower <- lowerBound.
  6.      upper   <- upperBound.
  7.      step    <- stepSize
  8. |  
  9.    size   
  10.      ^ ((step strictlyPositive) 
  11.           ifTrue:  [upper < lower]
  12.           ifFalse: [lower < upper] )
  13.         ifTrue:  [ 0 ]
  14.         ifFalse: [upper - lower // step + 1]
  15. |  
  16.    inRange: value
  17.      ^ (step strictlyPositive)
  18.         ifTrue:  [(value >= lower) and: [value <= upper]]
  19.         ifFalse: [(value >= upper) and: [value <= lower]]
  20.    first
  21.      current <- lower.
  22.      ^ (self inRange: current) ifTrue: [current]
  23. |
  24.    last
  25.      current <- upper.
  26.      ^ (self inRange: current) ifTrue: [current]
  27. |   
  28.    next
  29.      current <- current + step.
  30.      ^ (self inRange: current) ifTrue: [current]
  31. |
  32.    at: index ifAbsent: exceptionBlock   ! val !
  33.      val <- lower + (step * (index - 1)).
  34.      ^ (self inRange: val)
  35.         ifTrue: [ val ]
  36.         ifFalse: [exceptionBlock value]
  37.    printString
  38.      ^ 'Interval ', lower printString , ' to ',
  39.                     upper printString , ' by ' , step printString 
  40. |   
  41.    coerce: newcollection
  42.      ^ newcollection asArray
  43. |   
  44.    at: index put: val
  45.      ^ self error: 'cannot store into Interval'
  46. |   
  47.    add: val
  48.      ^ self error: 'cannot store into Interval'
  49. |   
  50.    removeKey: key ifAbsent: exceptionBlock
  51.      self error: 'cannot remove from Interval'.
  52.      ^ exceptionBlock value
  53. |
  54.    deepCopy
  55.      ^ lower to: upper by: step
  56. |   
  57.    shallowCopy
  58.      ^ lower to: upper by: step
  59. ]
  60.